//渠道
/*ascription2()
function ascription2() {
    $.ajax({
        type: "post",
        url: root.dockingplatform.allChannelConfig,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
        },
        success: function (data) {
            console.log(data)
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#ascription_lsit").html(interText(dataInter));
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });


}*/

function  chooseProject(){
    var chooseData={
        "projectId":$("#projectId").val(), //项目编码
        "projectName":$("#projectName").val(), //项目名称
        "projectBelongName":$("#projectBelongName").val(),//所属分公司
        "receiverCompanyName":$("#receiverCompanyName").val(),//收货公司名称
        "invoiceCompanyName":$("#invoiceCompanyName").val(),//开票公司名称
    };
    localStorage.setItem("chooseData",JSON.stringify(chooseData));
    layer_show('选择渠道','chooseChannel.html', 1200, 445);
}
//保存
interface();
function interface(){
        $.ajax({
            url:root.dockingplatform.queryAllChannelInterfaceByChannelId,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data: JSON.stringify({
                "id": parseInt(sessionStorage.getItem("id")),
            }),
            success: function (data) {
                if(data.errorCode==0){
                    couponlist(data);
                }else {
                    $.Huimodalalert(data.msg,2000);
                }
            },
            error: function () {
                $.Huimodalalert('保存失败！',2000);
            }
        })
}

// 提示信息
function showmsg(msg,icon){
    layer.msg(msg, {
        icon:icon,
        time: 1000
    },function(){
        if(icon==6){
            var index = parent.location.reload();
            parent.layer.close(index);
        }
    });
}

function couponlist(data, index) {
    console.log(data)
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
    $.each(data.data, function (i, field) {
        var rowData = new Array();
        rowData.push(field.interfaceCode);
        rowData.push(field.interfaceUrl);
        rowData.push(field.interfaceName);
        rowData.push(field.id);
        dataSource.push(rowData);
    });

    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            "fnDrawCallback": function () {
                this.api().column(0).nodes().each(function (cell, i) {
                    cell.innerHTML = i + 1;
                });
            },
            data: dataSource,
            columnDefs: [
                {
                    "sTitle": "序号",
                    "sClass": "dt-center",
                    "bSortable": false,
                    "sWidth": "4%",
                    "data": null,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span class=""><a onclick="editInterface(\'新增接口\',\'addInterface.html\',\'' + row[0] + '\',\'' + row[1] + '\')">编辑</a></span><span>&nbsp&nbsp<a onclick="detaildelete(\'' + row[3] + '\')">删除</a></span>';
                        return val;
                    },
                    "targets": 4
                },


            ]
        });
    }
}


function editInterface(title,url,interfaceCode,interfaceUrl) {
    sessionStorage.setItem("interfaceUrl",interfaceUrl);
    sessionStorage.setItem("interfaceCode",interfaceCode);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

function addProject(title,url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url,
        end:function () {
            location.reload();
        }
    });
    layer.full(index);
}



function detaildelete(id){
    layer.confirm('确认删除该项目？',function () {
        $.ajax({
            contentType: 'application/json',
            url: root.dockingplatform.deleteChannelInterface,
            cache: false,  //禁用缓存
            type: "POST",
            data:JSON.stringify({
                "id": id,
            }),
            success: function (data) {
                if(data.errorCode==0){
                    layer.msg("删除成功", {icon: 1, time: 2000});
                    interface();
                }else {
                    layer.msg(data.msg, {icon: 1, time: 2000});
                }
            }
        });

    })

}


function getChannelCode() {
    alert(1)
    $.ajax({
        url:root.dockingplatform.saveSubChannelConfigInfo,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "customerId": $("#customerId").val(),
        }),
        success: function (data) {
            if(data.errorCode==0){
                $("#customerId").val(localStorage.getItem("choosecustomerId"));
            }else {
                $.Huimodalalert(data.msg,2000);
            }
        },
        error: function () {
            $.Huimodalalert('保存失败！',2000);
        }
    })
}